/**
 * @author iLakeYC
 */
+function ($) {
    "use strict";

    jQuery.valueTransfer = {
        setValue: function (pageId, object) {
            window.sessionStorage[pageId] = JSON.stringify(object);
        },

        get: function (pageId) {
            var j = window.sessionStorage.getItem(pageId);
            if (j) {
                return JSON.parse(j);
            } else {
                return null;
            }
        },

        clear: function (pageId) {
            $.valueTransfer.setValue(pageId, null);
        }
    };

    jQuery.pacefulIms = {
        get: function () {
            var ims = window.localStorage.getItem("ACCESS_IMS");
            if (ims == null) {
                ims = Date.now();
                window.localStorage["ACCESS_IMS"] = ims;
            }
            return ims;
        }
    };

    /**
     * User information will storage here. Data structure same like api response.
     *
     * Please mind the property naming when you call the
     * function: <code>setProperty</code> or <code>setProperties</code>.
     * Example: user_name. Don't use Camel-Case.
     * */
    jQuery.pacefulUser = {
        storage: function (userInfo) {
            var s = JSON.stringify(userInfo);
            window.localStorage["USER_INFO"] = s;
        },
        get: function () {
            var userJson = window.localStorage.getItem("USER_INFO");
            return JSON.parse(userJson);
        },
        setProperty: function (key, value) {
            var u;
            if ($.pacefulUser.isEmpty()) {
                u = {}
            } else {
                u = $.pacefulUser.get();
            }
            u[key] = value;
            $.pacefulUser.storage(u);
            return u;
        },
        setProperties: function (pairs) {
            var u;
            if ($.pacefulUser.isEmpty()) {
                u = {}
            } else {
                u = $.pacefulUser.get();
            }
            for (var key in pairs) {
                u[key] = pairs[key];
            }
            $.pacefulUser.storage(u);
            return u;
        },
        isEmpty: function () {
            var s = window.localStorage.getItem("USER_INFO") || "";
            return s == "";
        },
        isNotEmpty: function () {
            return !$.pacefulUser.isEmpty();
        },

        accessToken: function () {
            if ($.pacefulUser.isEmpty()) {
                return null;
            }
            return $.pacefulUser.get()["token"];
        },

        tokenIsEmpty: function () {
            return ($.pacefulUser.accessToken() || "") == "";
        },

        tokenIsNotEmpty: function () {
            return !$.pacefulUser.tokenIsEmpty();
        }
    }

}($);